import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: XMQ
 * Date: 2022-05-29
 * Time: 8:35
 */

/**
 * String,StringBuffer,StringBuilder的区别
 * String里的内容不可以被改变 ,StringBuffer和StringBuilder内容可以被修改
 * StringBuffer和StringBuilder方法大致相同
 * StringBuffer是同步处理的，多线程情况下很安全(synchronized关键字修饰其方法)，StringBuilder没有同步处理，多线程情况下不安全
 */
public class Test {
    public static void main(String[] args) {
        StringBuilder stringBuilder = new StringBuilder();
        String s1 = "hello";
        stringBuilder.append(s1);
        stringBuilder.insert(0,"lyh ");
        stringBuilder.insert(1,1);
        stringBuilder.deleteCharAt(1);
        stringBuilder.setCharAt(0,'z');
        stringBuilder.replace(0,3,"zbl");
        int ch = stringBuilder.codePointAt(1);
        System.out.println(ch);
        stringBuilder.setLength(9);
        int count = stringBuilder.length();
        System.out.println(count);
        System.out.println(stringBuilder);
        stringBuilder.reverse();//字符串逆转
        String str = stringBuilder.toString();//StringBuilder转String
        stringBuilder.append("how are you");//String转StringBuilder
        int num = stringBuilder.capacity();
        System.out.println(num);//获取底层保存字符串空间的总大小
        System.out.println(stringBuilder);
    }
    public static void main5(String[] args) {
        String s1 = "";
        for (int i = 0; i < 100; i++) {
            s1 += i;//要创100个StringBuilder对象+100 toString new String对象
        }
        System.out.println(s1);
    }
    public static void main4(String[] args) {
        String s1 ="hello";
        String s2 = s1 + " word";
        System.out.println(s2);

//        StringBuilder stringBuilder = new StringBuilder();
//        stringBuilder.append("hello");
//        stringBuilder.append(" word");
//        String s3 = stringBuilder.toString();
//        System.out.println(s3);
    }
    public static void main3(String[] args) {
        String s1 = "hello";
        s1 += " word";
        System.out.println(s1);

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("hello");
        stringBuilder.append(" word");
        String s2 = stringBuilder.toString();
        System.out.println(s2);

        System.out.println("=========================");
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("hello");
        stringBuffer.append(" word");
        String s3 = stringBuffer.toString();
        System.out.println(s3);
    }
    public static boolean isLegal(char ch){
        if(ch >= 'a' && ch <= 'z' || ch >= '0' && ch <= '9'){
            return true;
        }else{
            return false;
        }
    }
    public static boolean isPalindrome(String s) {
        s = s.toLowerCase();
        int left = 0;
        int right = s.length()-1;
        while(left < right){
            //左边找合法字符
            if(left < right && !isLegal(s.charAt(left))){
                left++;
            }
            //右边找合法字符
            if(left < right && !isLegal(s.charAt(right))){
                right--;
            }
            if(s.charAt(left) != s.charAt(right)){
                return false;
            }
            left++;
            right--;
        }
        return true;
    }

    public static void main2(String[] args) {
        String s = "A man, a plan, a canal: Panama";
        String[] s2 = new String[]{s};
        for (int i = 0; i < s2.length; i++) {
            System.out.println(s2[i]);
        }
        System.out.println(s2[0]);
//        System.out.println(isPalindrome(s));
    }
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String s1 = in.nextLine();
            String[] s2 = s1.split(" ");
            System.out.println(s2[s2.length - 1].length());
        }
    }
}
